package com.mmsj;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class LoginActivity extends AppCompatActivity {

    EditText usernameEditText;
    EditText passwordEditText;
    Button loginButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);

        // 获取控件
         usernameEditText = findViewById(R.id.username);
         passwordEditText = findViewById(R.id.password);
        loginButton = findViewById(R.id.login);
        // 给登录按钮添加点击事件
        loginButton.setOnClickListener(v -> {
            String user = usernameEditText.getText().toString();
            String pass = passwordEditText.getText().toString();
            if (user.isEmpty() || pass.isEmpty()){
                usernameEditText.setError("用户名或密码不能为空");
            }
            else {
                if(user.equals("zhangsan") && pass.equals("12345")){
                    // 登录成功
                    // 跳转到主界面
                    // 创建意图
                    Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                    // 传递参数
                    Bundle bundle = new Bundle();
                    bundle.putString("username", user);
                    intent.putExtras(bundle);
                    // 启动意图
                    startActivity(intent);
                }
                else {
                    usernameEditText.setError("用户名或密码错误");
                }
            }
        });
    }
}